



var CONDITION_CTRL = "Crm:ConditionControl";
var PREDEFINED_LIST = "Crm:PredefinedList";
var CONDITIONGRP_CTRL = "Crm:ConditionGroup";
var CONDITIONFLD_CTRL = "Crm:ConditionField";
var ENTITY_LIST = "Crm:EntityList";
var ATTRIBUTE_LIST = "Crm:AttributeList";
var OPERATOR_LIST = "Crm:OperatorList";
var VALUE_CTRL = "Crm:Value";
var FREEFORM_CTRL = "Crm:Freeform";
var HIDDEN_CTRL = "Crm:Hidden";


var FIELD_GROUP = "fld";




var TimeoutExprEntity = "waittimeout";


function ConfigureToolbars(oCondition)
{
var oToolbar;
var sOp = "attachclick";

with(oCondition)
{

oToolbar = all.mnuBar5;
if(!IsNull(oToolbar))
{
oToolbar = all.mnuBar5.rows[0].cells[0];



PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnClear, id+".Clear()");
}
}
}






function CreateUI(sColId, elem)
{
switch(sColId)
{
case "colConfiguration":
return CreateConfigurationUI(sColId, elem);
case "colParameter":
return CreateEntityUI(sColId, elem);
}
return null;
}


function CreateEntityUI(sColId, elem)
{


var sAction = GetAction(elem);
if (sAction != "assign")
{
return null;
}

var sList = "";
var sPrimaryEntity = elem.Container.PrimaryEntityCode;

var i = 0;

while(true)
{
var sId = "id" + i;
var sName = "name" + i;

var sIdAttr = elem.ColumnData.attributes.getNamedItem(sId);
var sNameAttr = elem.ColumnData.attributes.getNamedItem(sName);
if (IsNull(sIdAttr) || IsNull(sNameAttr))
{
break;
}
if (sIdAttr.value == sPrimaryEntity)
{
sList += "<OPTION VALUE='" + sIdAttr.value + "'>" + CrmEncodeDecode.CrmHtmlEncode(sNameAttr.value) + "</OPTION>";
break;
}
i++;
}

return sList;
}


function GetAction(elem)
{
return elem.ParentControl.GetControl(0).DataValue;
}


function GetEntity(elem)
{
return elem.ParentControl.GetControl(1).DataValue;
}


function CreateConfigurationUI(sColId, elem)
{

var sAction = GetAction(elem);
if (sAction == "assign")
{

var oLookupControl = elem.Container.ClientCache.GetElement("ValueControl", "lookup");

var oLUImg = oLookupControl.rows[0].cells[1].firstChild;
oLUImg.lookuptypes = "8";
oLUImg.lookuptypenames = "systemuser";

var aiLookupTypes = oLUImg.lookuptypes.split(",");
oLUImg.lookuptypeIcons = elem.Container.ClientCache.LookupIcons[parseInt(aiLookupTypes[0], 10)];
for(var i=1; i< aiLookupTypes.length; i++)
{

oLUImg.lookuptypeIcons += ":" + elem.Container.ClientCache.LookupIcons[aiLookupTypes[i]];
}
oLUImg.lookupstyle = "single";
oLUImg.lookupclass = "AllLookups";
return oLookupControl.outerHTML;
}
else if (sAction == "create")
{
return "<TABLE style='TABLE-LAYOUT: fixed' cellSpacing=0 cellPadding=0 width='100%'><TBODY><TR><TD style='display:none'><INPUT type='text' class='ms-crm-Text' name='crmFormSubmitXml' value=''></TD><TD width=25><button id='configButton' class='txt' onclick='parentElement.parentElement.all.crmFormSubmitXml.value=LaunchCreateDialog(\"" + GetEntity(elem) + "\"); parentElement.parentElement.all.crmFormSubmitXml.fireEvent(\"onchange\");' Title='...'><img class='button'><br>...</button></TD></TR></TBODY></TABLE>"
}
}


function GetConfigurationXml(sColId, elem)
{

var sAction = GetAction(elem);
if (sAction == "assign")
{

var sValue;
var aoItems = elem.DataValue;
var iLen = aoItems.length;
if (iLen == 1)
{
sValue = aoItems[0].id;
}
return sValue;
}
else if (sAction == "create")
{
return elem.all.configButton.xml;
}
}

function InsertCustomizedDataSlug(slugValue, slugText)
{
if(!IsNull(slugValue))
{
slugValue = "<slug type=\"dynamic\" value=\"" + slugValue + "\"/>";
}
else
{
slugValue = "";
slugText  = "";
}

if(IsNull(slugText))
{
slugText = "";
}

var expressions = GetDurationExpression();
if(!IsNull(expressions) && !IsNull(expressions[0]))
{
slugValue = slugValue + expressions[0];
slugText = expressions[1] + slugText;
}

if(!IsNull(slugValue))
{
slugText =  "{" + slugText  + "}";
slugValue = "<slugbody><slugelement type=\"slug\">" + slugValue + "</slugelement></slugbody>";
InsertDataSlug(slugValue, slugText);
_currentValueControl.AttachDataSlugEvents();
}


}

function GetSlugDisplayText(slugXml, dataType)
{
GetTextForNode(slugXml,dataType);
}


function IsTimeoutExpr(key)
{
if(key.indexOf("#")!=0)
{
return false;
}

var arrToken = key.split("#");
if(arrToken.length<2)
{
return false;
}

if(arrToken[1].toLowerCase() == TimeoutExprEntity)
{
return true;
}

return false;
}


function IfTimeoutExpressionPresented(ParentControl)
{

var conditionGrp = ParentControl.ParentControl;
if(conditionGrp.GetNumControls()>0)
{
var cdnCtrl = conditionGrp.GetControl(0);

if(cdnCtrl == ParentControl)
{
return false;
}
return IsTimeoutCondition(cdnCtrl);
}
return false;
}




function CanSetTimeoutExpression(ParentControl)
{

var conditionGrp = ParentControl.ParentControl;
if(conditionGrp.GetNumControls()>2)
{
return false;
}

if(conditionGrp.GetNumControls()>0)
{
var cdnCtrl = conditionGrp.GetControl(0);

if(cdnCtrl == ParentControl)
{

return true;
}
}
return false;
}



function IsTimeoutCondition(cdnCtrl)
{
var iLen = cdnCtrl.GetNumControls();
var i = 0;

while(i<iLen)
{
var attrCtrl = cdnCtrl.GetControl(i);
if (!IsNull(attrCtrl) && attrCtrl.tagName == "AttributeList")
{
var sel = attrCtrl.GetSelectList();
if(!IsNull(sel.value))
{
var value = sel.value;
if(IsTimeoutExpr(value))
{
return true;
}
}
}
i++;
}
return false;
}

function IsOptionFiltered(optionValue)
{
return IsTimeoutExpr(optionValue);
}